package com.bskj.kuaifu.MQLister;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.bskj.common.utils.LogUtils;
import com.bskj.common.utils.StringUtils;
import com.bskj.kuaifu.service.IPayInfoService;
import com.rabbitmq.client.Channel;

/**
 * 无牌车支付回调
 * 
 * @author lishi
 * @date 2018年5月3日 上午9:17:39
 */
public class NoCarnoPayInfoMessageListener implements ChannelAwareMessageListener {
	@Autowired
	private IPayInfoService payInfoService;

	@Override
	public void onMessage(Message message, Channel channel) throws Exception {
		String xmlStr = new String(message.getBody(), "UTF-8");
		LogUtils.debug("NoCarnoPayInfoMessageListener ========== " + xmlStr);
		boolean deleteMessageFlag = true;
		try {
			JSONObject resp = JSONObject.parseObject(xmlStr);
			String return_code = resp.getString("state");
			if (!StringUtils.isBank(return_code) && return_code.equals("00")) {
            	payInfoService.noCarnoPayNotify(resp);
            }
		} catch (Exception e) {
			deleteMessageFlag = false;
			e.printStackTrace();
		}
		if (deleteMessageFlag) {
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} else {
			//multiple:false:是否批量.true:将一次性拒绝所有小于deliveryTag的消息，requeue：true被拒绝的是否重新入队列，
			channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
		}

	}

}
